import extensionData from '../lib/libraries/extensions/index.jsx';

const SHOW_INLINE_ALERT = 'scratch-gui/alerts/SHOW_INLINE_ALERT';
const CLOSE_INLINE_ALERT = 'scratch-gui/alerts/CLOSE_INLINE_ALERT';

const SHOW_STANDARD_ALERT = 'scratch-gui/alerts/SHOW_STANDARD_ALERT';
const CLOSE_STANDARD_ALERT = 'scratch-gui/alerts/CLOSE_STANDARD_ALERT';

const SHOW_EXTENSION_ALERT = 'scratch-gui/alerts/SHOW_EXTENSION_ALERT';
const CLOSE_EXTENSION_ALERT = 'scratch-gui/alerts/CLOSE_EXTENSION_ALERT';

import keyMirror from 'keymirror';
const AlertTypes = keyMirror({
    STANDARD: null,
    EXTENSION: null
});

/**
 * Initial state of alerts reducer
 *
 * {array} alertsList - list of alerts, each with properties:
 *  * closeButton (optional): bool indicating that we should show close button
 *  * content (optional): react element (a < />)
 *  * extentionId (optional): id string that identifies the extension
 *  * iconURL (optional): string
 *  * level (required): string:info/warn
 *  * message (optional): string
 *  * showReconnect (optional): bool
 */
const initialState = {
    inlineContent:'',
    inlineLevel:0,

    alertsList: [],
};

const reducer = function (state, action) {
    if (typeof state === 'undefined') state = initialState;
    switch (action.type) {        
    case SHOW_INLINE_ALERT: {//显示行内提示
        return {...state, inlineContent:action.content, inlineLevel:action.level};
    }
    case CLOSE_INLINE_ALERT: {//关闭行内提示
        return {...state, inlineContent:''};
    }

    case SHOW_STANDARD_ALERT: {//设置标准提示
        if (action.content == '') { return state; }

        const newAlert = {
            closeButton: true,
            content : action.content,
            level: action.level,
            icon : action.icon
        }
        var newList = state.alertsList.slice();
        newList.push(newAlert);
        return {...state, alertsList: newList};
    }
    case CLOSE_STANDARD_ALERT: {
        if (action.content == '') { return state; }

        return Object.assign({}, state, {
            alertsList: state.alertsList.filter(curAlert => (
                curAlert.content !== action.content
            ))
        });
    }

    case SHOW_EXTENSION_ALERT: {
        const extensionId = action.data.extensionId;
        if (extensionId) {
            const extension = extensionData.find(ext => ext.extensionId === extensionId);
            if (!extension) {
                return state; // if alert not found, show nothing
            }

            const newList = state.alertsList.slice();
            const newAlert = {
                level:"warn",
                extensionId: extensionId,
                extensionName: extension.name,
                content:extension.name,//仅仅只是作为关闭操作的 特殊码 用
                icon: extension.connectionSmallIconURL,
                closeButton: true,
                showReconnect: true
            };
            newList.push(newAlert);

            return {...state, alertsList: newList};
        }
    }
    case CLOSE_EXTENSION_ALERT: {
        return Object.assign({}, state, {
            alertsList: state.alertsList.filter(curAlert => (
                curAlert.extensionId !== action.extensionId
            ))
        });
    }
    default:
        return state;
    }
};

//显示行内提示：此类提示3秒自动消失
var inlineAlertTimeout = null;
const showInlineAlerts = function (dispatch, content, level) {
    clearTimeout(inlineAlertTimeout);
    inlineAlertTimeout = setTimeout(() => {inlineAlertTimeout=null;dispatch(closeInlineAlert())}, 3000);

    dispatch(showInlineAlert(content, level));    
};
//执行行内提示
const showInlineAlert = function (content, level) {
    return {type: SHOW_INLINE_ALERT, content, level};
};
//关闭行内提示
const closeInlineAlert = function () {
    return {type: CLOSE_INLINE_ALERT};
};


//显示标准提示：level:"warn"/"info",icon:"spinner"/icon的URL
const showStandardAlert = function (content, level, icon) {
    return {type: SHOW_STANDARD_ALERT, content, level, icon};
};
//关闭标准提示:
const closeStandardAlert = function (content) {
    return {type: CLOSE_STANDARD_ALERT, content};
};


/**
 * 显示扩展提示
 * @param {object} data - data for the alert
 * @param {string} data.message - message for the alert
 * @param {string} data.extensionId - extension ID for the alert
 * @return {object} - an object to be passed to the reducer.
 */
// const showExtensionAlert = function (id) {
const showExtensionAlert = function (data) {
    
    // var data = extensionData.find(ext => ext.extensionId === id);
    // if (!data) {
        //     return {type: (SHOW_EXTENSION_ALERT+"NULL")}; // if alert not found, show nothing
        // }
        
    return {type: SHOW_EXTENSION_ALERT,data};
};
//关闭扩展提示
const closeExtensionAlert = function (extensionId) {
    return {type: CLOSE_EXTENSION_ALERT,extensionId};
};


export {
    reducer as default,
    initialState as alertsInitialState,
    
    showInlineAlerts,//会自动关闭

    showStandardAlert,
    closeStandardAlert,

    showExtensionAlert,
    closeExtensionAlert
};
